import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class FileUtils {

	public static void appendToFile(String logFileName, String msg) {
		try {
			File logFile=new File(logFileName);
			mkDirsIfReq(logFile);
			FileWriter out= new FileWriter(logFile,true);
			out.write(msg);
			out.close();
		} catch (IOException e) {
			System.out.println("Error");
		}
		
	}
	public static List<File> readFolderContent(String path){
		File folder= new File(path);
		File[] listOfFiles= folder.listFiles();
		return Arrays.asList(listOfFiles);
	}
	private static void mkDirsIfReq(File newFile){
		File dirNew= newFile.getParentFile();
		if(!dirNew.exists()){
			dirNew.mkdirs();
		}
	}
	public static boolean copyAndSaveFilesInCorrectFolders(File oldFile,
			File newFile) {
		FileReader in;
		FileWriter out;
		mkDirsIfReq(newFile);
		//make dirs
		try {
			in = new FileReader(oldFile);
			out = new FileWriter(newFile);
			int c;
			while ((c = in.read()) != -1)
				out.write(c);
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			Logger.log(MsgType.ERROR, "File "+oldFile+" Not Found");
		} catch (IOException e) {
			Logger.log(MsgType.ERROR, "IO Exception on writing file "+newFile);
		}
		Logger.log(MsgType.SUCCESS, "File :"+oldFile+" copied to "+ newFile);
		return true;
	}

}
